package model

import (
	"github.com/jmoiron/sqlx"
	. "zimuzu_cvn_web_api/pkg/e"
	"zimuzu_cvn_web_api/pkg/errno"
)

type UsdtAmount struct {
	Uid           int64   `db:"uid"`
	Balance       float64 `db:"balance"`
	FrozenBalance float64 `db:"frozen_balance"`
	CreateTime    int64   `db:"create_time"`
	UpdateTime    int64   `db:"update_time"`
	SignString    string  `db:"sign_string"`
	SignVersion   string  `db:"sign_version"`
}

type UpdateUsdtAmountDb struct {
	Uid          int64   `db:"uid"`
	Amount       float64 `db:"balance"`
	FrozenAmount float64 `db:"frozen_balance"`
	SignString   string  `db:"sign_string"`
	SignVersion  string  `db:"sign_version"`
}

//获取账户信息
//Auth: TXG
func GetUsdtAmount(db sqlx.Queryer, uid int64) (qi UsdtAmount, err error) {
	err = sqlx.Get(db, &qi, "SELECT uid,balance,frozen_balance,create_time,update_time,sign_string,sign_version FROM usdt_amount WHERE uid = ? ", uid)
	if err != nil {
		Log("err", err)
	}
	return qi, err
}

//修改账户信息
//Auth: TXG
func UpdateUsdtAmount(db sqlx.Execer, data UpdateUsdtAmountDb) (err error) {
	res, err := db.Exec("UPDATE usdt_amount SET balance = balance + ?, frozen_balance = frozen_balance + ?, sign_string = ?, sign_version = ? WHERE uid = ?", data.Amount, data.FrozenAmount, data.SignString, data.SignVersion, data.Uid)
	if err != nil {
		Log("err", "UpdateAmountBlance:", data.Uid, err.Error())
		return err
	}
	row, err := res.RowsAffected()
	if err != nil {
		Log("err", "UpdateAmountBlance:", data.Uid, err.Error())
		return err
	}
	if row != 1 {
		Log("err", "UpdateAmountBlance:", data.Uid, "影响行数为:", row)
		err = errno.UpdateDataErr
		return err
	}
	return nil
}
